<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="layer/JQuery/jquery-3.1.1.min.js"></script>
    <script src="layer/layer/layer.js"></script>
    <link rel="stylesheet" href="layui/css/layui.css"/>
    <script src="layui/layui.js"></script>

    <style>
        .layui-input-block {
            margin-right: 300px;
        }

        #file {
            margin: 5px;
            width: 200px;
        }
    </style>
</head>

<body>
<form class="layui-form" th:action="@{/teacher}" method="post" enctype="multipart/form-data">
    <div class="layui-form-item" style="margin-top: 10px">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block">
            <input type="text" id="tid" name="tid" lay-verify="required|number" placeholder="请输入编号(8位)"
                   autocomplete="off"
                   class="layui-input" minlength="8" maxlength="8"
                   onkeyup="(this.v=function(){this.value=this.value.replace(/[^0-9-]+/,'');}).call(this)"
                   onblur="this.v();">
        <span id="ts"></span>
        </div>
    </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" id="tname" name="tname" lay-verify="required" placeholder="请输入姓名" autocomplete="off"
                   class="layui-input" maxlength="8">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">选择图片</label>
        <div class="layui-input-block">
            <input type="file" id="file" name="file" autocomplete="off">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">所属类别</label>
        <div class="layui-input-block">
            <select name="ttid" lay-verify="required">
                <option value=""></option>
                <option th:each="talent:${talentList}" th:text="${talent.tname}"
                        th:value="${talent.ttid}"></option>
            </select>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">信息来源</label>
        <div class="layui-input-block">
            <input type="text" id="tsource" name="tsource" placeholder="请输入信息来源" autocomplete="off"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">个人简介</label>
        <div class="layui-input-block">
            <textarea id="edit" name="tintro" style="display: none;" class="field-content"
                      lay-verify="content"></textarea>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" id="addbutton" lay-submit="" lay-filter="demo">提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            <a th:href="@{/teachers/1}">
                <input type="button" class="layui-btn layui-btn-primary" value="返回" style="margin-left: 9px;"></input>
            </a>
        </div>
    </div>

</form>

<script>
    layui.use('form', function () {
        var form = layui.form;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            layer.msg(JSON.stringify(data.field));
            return false;
        });
    });

    layui.use(['layer', 'layedit', 'form', 'jquery'], function () {
        var form = layui.form, layer = layui.layer;
        var $ = layui.$;
        var layedit = layui.layedit;
        var index = layedit.build('edit', {width: 400}); //建立编辑器
        form.verify({
            content: function (value) {
                return layedit.sync(index);
            }
        });
    });

    //判断新要添加的teacherId是否存在
    $("#tid").blur(function () {
        //必须取值，不然传不到值
        var tid = $("#tid").val();
        //用ajax去数据库匹配，
        $.ajax({
            url: "judgeTid", //要处理的页面
            type: "POST",               //提交方式
            dataType: "TEXT",          //返回的数据类型，TEXT字符串 JSON返回JSON XML返回XML；dataType中T要大写！！
            data: {tid: tid},              //要传过去的数据
            success: function (data) {          //回调函数，data为形参，是从login-cl.php页面返回的值
                if (data.trim() == "FALSE")           //trim()去空格，因为会返回全部内容，包括空格回车等，所以
                {
                    $("#ts").html("此编号已存在，请更换编号！");
                    $("#ts").css("color", "red");
                } else if (data.trim() == "ERROR") {
                } else if (data.trim() == "OK") {
                    $("#ts").html("此编号没有占用，可以添加！");
                    $("#ts").css("color", "green");
                } else {
                }

            }
        });
    });

    // 判断输入是否为空字符串
    $("#tname").blur(function () {
        var temp = $("#tname").val();
        if(temp.replace(/[ ]/g, '')==''){
            $("#tname").val("")
            // alert("字符串为空或者全为空格");
        }
    });

    $("#tsource").blur(function () {
        var temp = $("#tsource").val();
        if(temp.replace(/[ ]/g, '')==''){
            $("#tsource").val("")
            // alert("字符串为空或者全为空格");
        }
    });
</script>
</body>

</html>